import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CustomerEntity } from './customer.entity';
@Injectable()
export class CustomerService {
    constructor(
        @InjectRepository(CustomerEntity)
        private readonly customerRepository: Repository<CustomerEntity>
    ) {}

    async create(post):Promise<any> {
        await this.customerRepository.save(post)
        return {
            message:'用户信息提交成功'
        }
    }

    async remove(id):Promise<any> {
        const existCustomer = await this.customerRepository.findOne(id)
        if(!existCustomer){
            throw new HttpException(`id为${id}的客户信息不存在`, HttpStatus.BAD_REQUEST)
        }
        await this.customerRepository.remove(existCustomer)
        return {
            message:'用户信息删除成功'
        }
    }

    async update(id, post): Promise<any> {
        const existCustomer = await this.customerRepository.findOne(id)
        if(!existCustomer){
            throw new HttpException(`id为${id}的客户信息不存在`, HttpStatus.BAD_REQUEST)
        }
        Object.assign(post,{id:Number(id)})
        await this.customerRepository.save(post)
        return {
            message:'用户信息更新成功',
            id: id
        }
    }
}
